<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Page;
//公告管理
class ArticleController extends CommonController {
//公告的显示控制器
	public function index(){
		$this -> assign('title','公告管理');
		//加入公告的信息
		$mod = M('article');
		//判断是否进行选择每页条数
		//设置查询
		//查询内容				
 			if(empty($_GET['search'])){
 				$search = '';
 			}else{
 				$search = $_GET['search'];
 			} 	
 		//查询页数
 			if(empty($_GET['num'])){
 				$num = 10;
 			}else{
 				$num = I('get.num');
 			} 
 			//设置分页的配置
 			$page = new Page($mod->count(),$num);
 			$page->setConfig('prev','上一页');
 			$page->setConfig('next','下一页');
 			//从page里面找到limit的配置 		
 			$data['title'] = array('like',"%$search%");
 			$articleData = $mod->where($data)->order('article_id desc')->limit($page->firstRow,$page->listRows)->select();
			
		//分页链接
		$url = $page->show();
		//讲分页链接分配变量
		$this->assign('num',$num);
		$this->assign('search',$search);
		$this->assign('url',$url);
		
		//将信息分配到页面		
		$this -> assign('articleRes',$articleData);
		$this -> display('');
	}
//公告的添加
	public function add(){	
			
		$this -> assign('title','公告添加');

		$this -> display('');
	}
	public function insert(){
		$articleData = $_POST;
		$articleData['cat_id'] = '13';
		$articleData['add_time'] = time();
		//实例化
		$mod = M('article');
		$mod -> startTrans();
		//插入日志
		$admin_log = M('admin_log');
		$ad_log['user_name'] = $_SESSION['admin']['user_name'];
		$ad_log['log_time'] = time();
		$ad_log['user_id'] = $_SESSION['admin']['user_id'];
		$ad_log['log_info'] = '公告添加操作';
		$ad_log['ip_address']= get_client_ip();
		$res2 = $admin_log -> add($ad_log);
		$res = $mod -> add($articleData);
		if($res>0 && $res2){
			$mod -> commit();
			$this -> success('添加成功',U('Admin/Article/index'));
		}else{
			$mod -> rollback();
			$this -> error('添加失败',U('Admin/Article/index'));
		}		

	}
	public function delete(){
		$article_id = I('get.article_id');
		$mod = M('article');
		$mod -> startTrans();
		$res = $mod->where('article_id='.$article_id)->delete();		
		 //插入日志
		$admin_log = M('admin_log');
		$ad_log['user_name'] = $_SESSION['admin']['user_name'];
		$ad_log['log_time'] = time();
		$ad_log['user_id'] = $_SESSION['admin']['user_id'];
		$ad_log['log_info'] = '公告删除操作';
		$ad_log['ip_address']= get_client_ip();
		$res2 = $admin_log -> add($ad_log);
		if($res>0 && $res2>0){
			$mod -> commit();
			$this -> success('删除成功',U('Admin/Article/index'));
		}else{
			$mod -> rollback();
			$this -> error('删除失败',U('Admin/Article/index'));
		}	
	}
	public function edit(){
		$this -> assign('title','公告修改');
		$article_id = I('get.article_id');
		$data = M('article')->where('article_id='.$article_id)->find();
		$this -> assign('data',$data);
		$this -> display();
	}
	public function update(){
		//修改公告信息		
		$articleData = $_POST;
		// dump(I('post.'));
		$articleData['cat_id'] = '13';
		$articleData['article_status'] = I('post.article_status');
		$articleData['add_time'] = time();
		//获取修改公告的id
		$id = I('get.article_id');
		$mod = M('article');
		$mod -> startTrans();
		$res = $mod -> where('article_id='.$id)->save($articleData);
		//封装日志
		$admin_log = M('admin_log');
		$ad_log['user_name'] = $_SESSION['admin']['user_name'];
		$ad_log['log_time'] = time();
		$ad_log['user_id'] = $_SESSION['admin']['user_id'];
		$ad_log['log_info'] = '公告删除操作';
		$ad_log['ip_address']= get_client_ip();
		$res2 = $admin_log -> add($ad_log);

		if($res>0 && $res2>0){
			$mod -> commit();
			$this -> success('修改成功',U('Admin/Article/index'));
		}else{
			$mod -> rollback();
			$this -> error('修改失败',U('Admin/Article/index'));
		}	
	}
	//查看公告信息
	public function see(){
		$this ->assign('title','公告查看');
		$mod = M('article');
		$id = I('get.article_id');
		$data = $mod -> where('article_id='.$id)->find();
		$this -> assign('data',$data);
		$this -> display('');
	}
	
}